package longrise.phone.com.bjjt_jyb.Utils;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.ContextWrapper;



/**
 * Created by Administrator on 2016/5/18.
 */
public class DialogUtils {

    private static Dialog processDialog;

    //显示加载对话框
    public static void showDialog(Context context) {
        if (!checkContextFinish(processDialog)) {
            processDialog = null;
            processDialog = UiUtil.showProcessDialog(context, "加载数据中...");
        }
        if (!processDialog.isShowing()) {
            processDialog.show();
        }
    }

    /**
     * 判断当前dialog依附的activity是否已销毁
     *
     * @return
     */
    public static boolean checkContextFinish(Dialog dialog) {
        if (null == dialog) {
            return false;
        }
        ContextWrapper contextWrapper = (ContextWrapper) dialog.getContext();
        if (null == contextWrapper) {
            return false;
        }
        Activity activity = (Activity) contextWrapper.getBaseContext();
        if (null == activity || activity.isFinishing()) {
            return false;
        }
        return true;
    }

    //关闭加载对话框
    public static void closeDialog() {
        if (null != processDialog) {
            if (processDialog.isShowing()) {
                processDialog.dismiss();
            }
        }
    }

    public static void destroyDialog() {
        closeDialog();
        processDialog = null;
    }
}
